/*
 * @Author       : NaN 353362604@qq.com
 * @Date         : 2023-01-12 09:35:49
 * @LastEditTime : 2023-07-10 08:48:22
 * @LastEditors  : NaN 353362604@qq.com
 * @FilePath     : \AdminStration\src\Common\Router\MenuBoxList.jsx
 * @Description  :
 */
/**
 *  TODO 后台通用菜单配置
 */
import axios from 'axios'
import { API_LIST } from '../Api/Api'
import { BuiltInData, commonTableList, breadthSearch } from '../Modal/config'

const MenuListConfig = {}

export const MenuBoxList = async (url, OptionsEvent, uuid, actionRef) => {
    const ClientData = JSON.parse(localStorage.getItem('ClientData'))
    const MENU = JSON.parse(sessionStorage.getItem('MENU'))
    const Permission = breadthSearch(MENU, 'targetUrl', url)?.children
    console.info('ROT页面路径 -', url)
    // console.info('权限', Permission)
    if (MenuListConfig[url]) {
        let MenuList = await MenuListConfig[url]({
            uuid,
            url,
            OptionsEvent,
            commonTableList,
            BuiltInData,
            ClientData,
            Permission,
            actionRef,
        })
        return MenuList
    } else {
        return false
    }
}

// ?~@ 平级列表
export const setValueEnumData = async ({ url, data = {}, path = [] }) => {
    let dataList = {}
    let list = await (await axios.get(API_LIST(path)[url], { params: data })).data?.list

    if (list) {
        switch (url) {
            case 'getPerson':
                list.forEach(
                    item =>
                        (dataList = { ...dataList, [item.uuid]: item.organizationName + item.tableName + item.mobile }),
                )
                break
            default:
                list.forEach(item => (dataList = { ...dataList, [item.uuid]: item.tableName }))
                break
        }
    }
    return dataList
}
// ?~@ 树形列表

const NotTree = [' ', 'getBadaStatOrganizationPppListTree', 'getBadaStatOrganizationBuildListTree']

export const setFieldPropsOptions = async ({ url, data = {}, path = [] }) => {
    let dataTree = await (await axios.get(API_LIST(path)[url], { params: { ...data } })).data
    if (url !== 'getInfoTypeManage')
        dataTree =
            NotTree.indexOf(url) !== -1
                ? dataTree
                : [{ uuid: '00000000-0000-0000-0000-000000000000', tableName: '顶级菜单' }, ...dataTree]
    return dataTree
}

// ?~@ select排序
export const setSelectSort = async ({ data }) => {
    const keys = Object.keys(data)
    const SortValue = Object.values(data).sort((a, b) => a.localeCompare(b))
    const res = {}
    for (let i in SortValue) {
        keys.forEach(item => {
            if (SortValue[i] === data[item]) res[item] = SortValue[i]
        })
    }
    return res
}
